﻿using System;
using Autodesk.Revit.DB;

namespace YJKRevitKernel.ElementCreatUtility
{
	public static class LineTypeCreatUtilityMethod
	{
        public static GraphicsStyle CreateLineType(Autodesk.Revit.DB.Document doc, string name, Autodesk.Revit.DB.Color color, int widIndex, GraphicsStyleType type = (GraphicsStyleType)1)
		{
			GraphicsStyle result = null;
			Transaction transaction = new Transaction(doc, "CreateLineType");
			try
			{
				transaction.Start();
				Category category = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Lines);
				Category category2;
				if (category.SubCategories.Contains(name))
				{
					category2 = category.SubCategories.get_Item(name);
				}
				else
				{
					category2 = doc.Settings.Categories.NewSubcategory(category, name);
				}
				category2.LineColor = color;
				category2.SetLineWeight(widIndex, type);
				result = category2.GetGraphicsStyle(type);
				transaction.Commit();
			}
			catch
			{
				if (transaction.HasStarted())
				{
					transaction.RollBack();
				}
				result = null;
			}
			return result;
		}

		public static void DeleteLineType(Autodesk.Revit.DB.Document doc, string name)
		{
			Transaction transaction = new Transaction(doc, "EraseLineType");
			try
			{
				transaction.Start();
				Category category = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Lines);
				if (category.SubCategories.Contains(name))
				{
					Category category2 = category.SubCategories.get_Item(name);
					doc.Delete(category2.Id);
				}
				transaction.Commit();
			}
			catch
			{
				if (transaction.HasStarted())
				{
					transaction.RollBack();
				}
			}
		}
	}
}
